﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using TarreOA.TarreOAModels;
using TarreOA.TarreOABLL;

public partial class admin_UpdateProduct : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
        {
            Session["ShowMessage"] = "用户未登录，请重新登录!";
            Session["Backurl"] = "Login.aspx";
            Response.Redirect("~/Main.aspx");
        }
        else
        {
            TarreOA.TarreOAModels.User sucessUser = (User)Session["user"];
            bool userFlag = false;
            if (sucessUser.PopedomGroup.PopedomList != null)
            {
                foreach (Popedom var in sucessUser.PopedomGroup.PopedomList)
                {
                    if (var.PopedomId == 40)
                    {
                        userFlag = true;
                        break;
                    }
                }
            }
            if (!userFlag)
            {
                Session["ShowMessage"] = "用户没有该权限，请与管理员联系!";
                Session["Backurl"] = "Right.aspx";
                Response.Redirect("~/Message.aspx");
            }
        }
        if (!IsPostBack)
        {
            Product product = ProductManager.FindByProductId(Convert.ToInt32(Request.QueryString["pid"]));
            txtProductName.Text = product.ProductName;
            txtProductType.Text = product.ProductType;
            txtComputation.Text = product.Computation;
            txtCostPrice.Text = product.CostPrice.ToString();
            txtSalePrice.Text = product.SalePrice.ToString();
            txtDescription.Text = product.Description;
            txtSupply.Text = product.Supply.SupplyName;
            hidSupplyId.Value = product.Supply.SupplyId.ToString();
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Product product = new Product();
        product.ProductName = txtProductName.Text;
        product.ProductType = txtProductType.Text;
        product.Computation = txtComputation.Text;
        if (txtCostPrice.Text != null && txtCostPrice.Text != "")
        {
            product.CostPrice = Convert.ToDecimal(txtCostPrice.Text);
        }
        else
        {
            product.CostPrice = 0;
        }
        if (txtSalePrice.Text != null && txtSalePrice.Text != "")
        {
            product.SalePrice = Convert.ToDecimal(txtSalePrice.Text);
        }
        else
        {
            product.SalePrice = 0;
        }
        product.Description = txtDescription.Text;
        if (hidSupplyId.Value != "")
        {
            Supply supply = new Supply();
            supply.SupplyId = Convert.ToInt32(hidSupplyId.Value);
            product.Supply = supply;
        }
        product.Productid = Convert.ToInt32(Request.QueryString["pid"]);
        if (ProductManager.UpdateProduct(product))
        {
            Response.Redirect("ProductList.aspx?pageIndex="+Request.QueryString["pageIndex"]);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "alert('修改失败！请重试！')", true);
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("ProductList.aspx?pageIndex=" + Request.QueryString["pageIndex"]);
    }
}
